package com.community.common.core.corethreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorUtils {

    private static final Integer code = 10;
    private static final Integer max = 30;
    private static final Integer keeptime = 3000;
    private static final Integer queueLength = 2000;
    private static ThreadPoolExecutor threadPoolExecutor = null;
    static {
        threadPoolExecutor = new ThreadPoolExecutor(
                code, // 核心线程池数量(线程池中维护的最少数量)
                max, // 线程池的最大数量(包含了核心数量，核心线程数量被用完了，需要创建新的线程)
                keeptime, // 时间(线程空闲的时间，超过这个时间就从线程池中删除线程)
                TimeUnit.MILLISECONDS, // 时间单位
                new ArrayBlockingQueue(queueLength), // 队列(没有空闲线程分配是会暂时压入队列等待),只有队列满了以后才会创建新的线程
                Executors.defaultThreadFactory(), // 线程工厂
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略(线程数量已经达到了最大数量同时没有空闲线程，而且队列也满了就会拒绝)
        );
    }

    public static ThreadPoolExecutor getThreadPoll() {
        return threadPoolExecutor;
    }

}
